//
//  MFriendDetailViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/10.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MFriendDetailViewController.h"
#import "MVIPCardService.h"
#import "MDDataBaseUtil.h"
#import "MMerchantViewController.h"
#import "MListModel.h"
#import "VIPCardReturnCashModel.h"
@interface MFriendDetailViewController ()<UITableViewDataSource, UITableViewDelegate>{


    MMerchantViewController *merchantVC;
}
@property (nonatomic, strong) MListModel *listModel;
- (IBAction)backAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *salemenNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *salemenAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *salemenPhoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *hasReturnCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *returnCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *pickupCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *reutrnCashStringLabel;
@property (weak, nonatomic) IBOutlet UIButton *merchantBtn;
- (IBAction)merchantBtnAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *returnCashBtn;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITableView *myTabelView;

@property (nonatomic, strong) NSMutableArray *vipReturnMutArray;
@end

@implementation MFriendDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self _initButton];
}
- (void)_initButton{

    [self.merchantBtn.layer setCornerRadius:3.0];
    [self.merchantBtn.layer setMasksToBounds:YES];
    [self.merchantBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.merchantBtn.layer setBorderWidth:1.0];
    
    [self.returnCashBtn.layer setCornerRadius:3.0];
    [self.returnCashBtn.layer setMasksToBounds:YES];
    [self.returnCashBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.returnCashBtn.layer setBorderWidth:1.0];
    
}
- (void)viewWillAppear:(BOOL)animated {

    if (self.vipCardListModel) {
        self.salemenNameLabel.text = self.vipCardListModel.salemanName;
        self.salemenAddressLabel.text = self.vipCardListModel.salemanAddress;
        self.salemenPhoneLabel.text = self.vipCardListModel.salemanPhone;
        self.hasReturnCashLabel.text = [NSString stringWithFormat:@"%@元",self.vipCardListModel.hasReturnCash];
        self.returnCashLabel.text = self.vipCardListModel.returnCash;
        self.pickupCashLabel.text = [NSString stringWithFormat:@"%@元",self.vipCardListModel.pickUpCash];
        self.reutrnCashStringLabel.text = [NSString stringWithFormat:@"%@折",self.vipCardListModel.returnPercentString];
        if ([self.vipCardListModel.isUse integerValue]) {
            [self.returnCashBtn setHidden:NO];
        } else {
        
            [self.returnCashBtn setHidden:YES];
            CGRect origion = self.contentView.frame;
            origion.size.height = 280;
            self.contentView.frame = origion;
            
            CGRect origionFrame = self.myTabelView.frame;
            origionFrame.origin.y = 280;
            origionFrame.size.height = self.myTabelView.frame.size.height + 20;
            self.myTabelView.frame = origionFrame;
        }
        [self getVIPCordModel];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 51.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREENWIDTH, 30)];
    titleLabel.text = @"VIP卡返现纪录";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont fontWithName:nil size:17.0];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:titleLabel];
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, SCREENWIDTH, 21)];
    titleView.backgroundColor = WHITESMOLKE;
    [headerView addSubview:titleView];
    
    UILabel *cashLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH/2, 21)];
    cashLabel.textAlignment = NSTextAlignmentCenter;
    cashLabel.text = @"金额(元)";
    cashLabel.font = [UIFont fontWithName:nil size:13.0];
    [titleView addSubview:cashLabel];

    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 0, SCREENWIDTH/2, 21)];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = @"使用日期";
    timeLabel.font = [UIFont fontWithName:nil size:13.0];
    [titleView addSubview:timeLabel];
    return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return self.vipReturnMutArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellStr = @"VIP_RETURN_CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        
        UILabel *cashLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH/2, 30)];
        cashLabel.textAlignment = NSTextAlignmentCenter;
        cashLabel.font = [UIFont fontWithName:nil size:12.0];
        cashLabel.tag = 101;
        [cell.contentView addSubview:cashLabel];
        
        UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 0, SCREENWIDTH/2, 30)];
        timeLabel.textAlignment = NSTextAlignmentCenter;
        timeLabel.tag = 102;
        timeLabel.font = [UIFont fontWithName:nil size:13.0];
        [cell.contentView addSubview:timeLabel];
    }
    VIPCardReturnCashModel *model = [[VIPCardReturnCashModel alloc]init];
    model = [self.vipReturnMutArray objectAtIndex:indexPath.row];
    
    ((UILabel *)[cell.contentView viewWithTag:101]).text = model.money;
    NSString *string1 = [model.createTime substringToIndex:10];
    NSString *string2 = [model.createTime substringFromIndex:10];
    ((UILabel *)[cell.contentView viewWithTag:102]).text = [NSString stringWithFormat:@"%@ %@",string1,string2];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)getVIPCordModel{

    [[MVIPCardService sharedClient] getMerchantInfoWithMerchantID:self.vipCardListModel.salemanUserID latitude:[MDDataBaseUtil txtLatitude] longtitude:[MDDataBaseUtil txtLongtitude] completion:^(MDetailMerchantModel *model){
        self.listModel = [[MListModel alloc]init];
        self.listModel.companyName = model.companyName;
        self.listModel.phone = model.phone;
        self.listModel.address = model.companyAddress;
        self.listModel.latitude = model.positionX;
        self.listModel.longtitude = model.positionY;
        self.listModel.saleManMemo = model.memo;
        self.listModel.createUserID = model.ID;
        self.listModel.distance = model.radius;
    
    } failure:^(NSString *error){
    
        
    }];
    
    [[MVIPCardService sharedClient] getVipCardComsumeListWithCardID:[self.vipCardListModel.ID integerValue] pageIndex:1 pageSize:20 userID:[[MDDataBaseUtil userID]integerValue] sortType:@"CreateTime|1" userName:nil completion:^(NSMutableArray *result){
    
        self.vipReturnMutArray = [NSMutableArray arrayWithArray:result];
        [self.myTabelView reloadData];
        
    } failure:^(NSString *error){
    
        
    }];
    
}
- (IBAction)merchantBtnAction:(UIButton *)sender {
    merchantVC = [[MMerchantViewController alloc]init];
    merchantVC.model = self.listModel;
    merchantVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:merchantVC animated:YES completion:nil];
}
@end
